package com.krain.ddbb.activity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.ddbb.R;
import com.krain.ddbb.adapter.UserIssueAdapter;
import com.krain.ddbb.adapter.UserWorkAdapter;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.UserIssueBean;
import com.krain.ddbb.entity.WorkNameBean;
import com.krain.ddbb.fragment.OrderListFragment;
import com.krain.ddbb.fragment.OrderListFragment_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

@EActivity(R.layout.activity_user_order)
public class UserOrderActivity extends BaseActivity {

    @ViewById(R.id.act_order_btn_issue)
    TextView mBtnIssue;//我发布的订单
    @ViewById(R.id.act_order_btn_serve)
    TextView mBtnServe;//我服务的订单
    OrderListFragment_ fragment_publish;
    OrderListFragment_ fragment_server;
    FragmentTransaction transaction;
    Bundle bundle_publish, bundle_server;
    int status;//记录切换的状态

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void onUserEvent(Object obj){
        loadFragment(fragment_publish);
    }

    @AfterViews
    void afterView() {
        bundle_publish = new Bundle();
        bundle_server = new Bundle();
        bundle_publish.putInt("type", OrderListFragment.TYPE_PUBLISH);
        bundle_server.putInt("type", OrderListFragment.TYPE_ROBED);
        fragment_publish = new OrderListFragment_();
        fragment_server = new OrderListFragment_();
        fragment_publish.setArguments(bundle_publish);
        fragment_server.setArguments(bundle_server);
        loadFragment(fragment_publish);
    }


    void loadFragment(Fragment fragmentShow) {
        transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.act_user_order_fl, fragmentShow);
        transaction.commit();
    }


    @Click(R.id.act_order_btn_issue)
    void clickToUserIssue() {
        if (status == OrderListFragment.TYPE_ROBED) {
            status = OrderListFragment.TYPE_PUBLISH;
            mBtnIssue.setBackgroundResource(R.drawable.shape_order_issue_bg);
            mBtnIssue.setTextColor(getResources().getColor(R.color.white));
            mBtnServe.setBackgroundResource(R.drawable.shape_order_serve_bg);
            mBtnServe.setTextColor(getResources().getColor(R.color.center_red));
            loadFragment(fragment_publish);
        }

    }

    @Click(R.id.act_order_btn_serve)
    void clickToUserServe() {
        if (status == OrderListFragment.TYPE_PUBLISH) {
            status = OrderListFragment.TYPE_ROBED;
            mBtnIssue.setBackgroundResource(R.drawable.shape_order_serve_btn_bg);
            mBtnIssue.setTextColor(getResources().getColor(R.color.center_red));
            mBtnServe.setBackgroundResource(R.drawable.shape_order_issue_btn_bg);
            mBtnServe.setTextColor(getResources().getColor(R.color.white));
            loadFragment(fragment_server);
        }

    }

    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, UserOrderActivity_.class);
        activity.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return "订单";
    }
}
